﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
using System.Reflection;

namespace kba.modbus.master
{
    public partial class mbmaster : Form
    {
        public mbmaster()
        {
            InitializeComponent();
        }

        private void modbusWebsiteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var resourceManager = kba.modbus.master.Properties.Resources.ResourceManager;
            var URL = resourceManager.GetString("modbusWebsite");                      
            builtInBrowser.Url = new Uri(URL);                        
            tabBrowser.Show();
            tabBrowser.BringToFront();
            //tabBrowser.Focus();
            tabBrowser.Select();
            

            //System.Diagnostics.Process.Start(URL);            
        }

        private void modbusProtocolSpecification(object sender, EventArgs e)
        {
            var resourceManager = kba.modbus.master.Properties.Resources.ResourceManager;
            var URL = resourceManager.GetString("modbusProtocolSpecification");
            builtInBrowser.Url = new Uri(URL);
            //System.Diagnostics.Process.Start(URL);            
        }

        private void modbusSerialLineProtocolAndImplementationGuideToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var resourceManager = kba.modbus.master.Properties.Resources.ResourceManager;
            var URL = resourceManager.GetString("modbusSerialLineProtocolAndImplementationGuide");
            builtInBrowser.Url = new Uri(URL);
            //System.Diagnostics.Process.Start(URL); 
        }

        private void modbusTCPIPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var resourceManager = kba.modbus.master.Properties.Resources.ResourceManager;
            var URL = resourceManager.GetString("modbusTCPIP");
            builtInBrowser.Url = new Uri(URL);
            //System.Diagnostics.Process.Start(URL);       
        }

        private void toolStripStatusLabel5_Click(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutModbusMasterSlaveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dlgAboutModbusMaster AboutBox = new dlgAboutModbusMaster();            
            AboutBox.ShowDialog();
        }

        private void connectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dlgModbusComConfig ModbusComConfig = new dlgModbusComConfig();
            ModbusComConfig.Show();

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deviceErrorlistBox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + " " + "Open File Menu Item");
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Modbus Workspace (*.mbws)|*.MBWS|" +
            "All files (*.*)|*.*";
            dlg.CheckFileExists = true;
            dlg.AddExtension = true;
            dlg.FileName = "Defualt.mbws";
            dlg.InitialDirectory = Application.StartupPath;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                //rtDoc.LoadFile(dlg.FileName);
                //rtDoc.Enabled = true;
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deviceErrorlistBox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + " " + "Save Menu Item Selected");
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "Save";
            dlg.Filter = "Modbus Workspace (*.mbws)|*.MBWS|" +
            "All files (*.*)|*.*";
            dlg.CheckFileExists = true;
            dlg.FileName = "Defualt.mbws";
            dlg.AddExtension = true;
            dlg.InitialDirectory = Application.StartupPath;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                //rtDoc.SaveFile(dlg.FileName);
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deviceErrorlistBox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + " " + "Save As menu item selected");
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Modbus Workspace (*.mbws)|*.MBWS|" +
            "All files (*.*)|*.*";
            dlg.CheckFileExists = true;
            dlg.FileName = "Defualt.mbws";
            dlg.AddExtension = true;
            dlg.Title = "Save As";
            dlg.InitialDirectory = Application.StartupPath;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                //rtDoc.SaveFile(dlg.FileName);
            }
        }

        private void disconnectToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Modbus Plugin (*.dll)|*.DLL|" +
            "All files (*.*)|*.*";
            dlg.Title = "Load Plugin";
            dlg.CheckFileExists = true;
            dlg.AddExtension = true;
            dlg.FileName = "";
            dlg.InitialDirectory = Application.StartupPath;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                //rtDoc.LoadFile(dlg.FileName);
                //rtDoc.Enabled = true;
            }
        }

        private void writeSingleCoilToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dlgWriteSingleCoil WriteSingleCoil = new dlgWriteSingleCoil();
            WriteSingleCoil.Show();
        }

        private void writeSingleRegisterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dlgWriteSingleRegister WriteSingleRegister =  new dlgWriteSingleRegister();
            WriteSingleRegister.Show();

        }

        private void readCoilsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dlgReadCoils ReadCoils = new dlgReadCoils();
            ReadCoils.Show();
        }

        private void loadScriptToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Modbus Script (*.mbs)|*.MBS|" +
            "All files (*.*)|*.*";
            dlg.CheckFileExists = true;
            dlg.Title = "Open Script";
            dlg.AddExtension = true;
            dlg.FileName = "Defualt.mbs";
            dlg.InitialDirectory = Application.StartupPath;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                //rtDoc.LoadFile(dlg.FileName);
                //rtDoc.Enabled = true;
            }
        }

        private void saveScriptToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Modbus Workspace (*.mbws)|*.MBWS|" +
            "All files (*.*)|*.*";
            dlg.CheckFileExists = true;
            dlg.FileName = "Defualt.mbws";
            dlg.AddExtension = true;
            dlg.Title = "Save Script";
            dlg.InitialDirectory = Application.StartupPath;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                //rtDoc.SaveFile(dlg.FileName);
            }
        }

        private void saveScriptAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Modbus Workspace (*.mbws)|*.MBWS|" +
            "All files (*.*)|*.*";
            dlg.CheckFileExists = true;
            dlg.FileName = "Defualt.mbws";
            dlg.AddExtension = true;
            dlg.Title = "Save Script As";
            dlg.InitialDirectory = Application.StartupPath;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                //rtDoc.SaveFile(dlg.FileName);
            }
        }

        private void readDiscreteInputsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dlgReadDiscreteInputs ReadDiscreteInputs = new dlgReadDiscreteInputs();
            ReadDiscreteInputs.Show(); 
        }

        private void readHoldingRegistersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dlgReadHoldingRegisters ReadHoldingRegisters = new dlgReadHoldingRegisters();
            ReadHoldingRegisters.Show();
        }

        private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deviceErrorlistBox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + " " + "Page Setup Menu Item Selected");
            // Initialize the dialog's PrinterSettings property to hold user 
            // defined printer settings.            
            PageSetupDialog1.PageSettings =
                new System.Drawing.Printing.PageSettings();

            // Initialize dialog's PrinterSettings property to hold user 
            // set printer settings.
            PageSetupDialog1.PrinterSettings =
                new System.Drawing.Printing.PrinterSettings();

            //Do not show the network in the printer dialog.
            PageSetupDialog1.ShowNetwork = false;

            //Show the dialog storing the result.
            DialogResult result = PageSetupDialog1.ShowDialog();

            // If the result is OK, display selected settings in 
            // ListBox1. These values can be used when printing the 
            // document. 
            if (result == DialogResult.OK)
            {
                object[] results = new object[]{ 
				PageSetupDialog1.PageSettings.Margins, 
				PageSetupDialog1.PageSettings.PaperSize, 
				PageSetupDialog1.PageSettings.Landscape, 
				PageSetupDialog1.PrinterSettings.PrinterName, 
				PageSetupDialog1.PrinterSettings.PrintRange};                
            }

        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deviceErrorlistBox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + " " + "Print Menu Item Selected");
            // Allow the user to choose the page range he or she would 
            // like to print.
            PrintDialog1.AllowSomePages = true;

            // Show the help button.
            PrintDialog1.ShowHelp = true;

            // Set the Document property to the PrintDocument for  
            // which the PrintPage Event has been handled. To display the 
            // dialog, either this property or the PrinterSettings property  
            // must be set 
            
            //PrintDialog1.Document = docToPrint;

            DialogResult result = PrintDialog1.ShowDialog();

            // If the result is OK then print the document. 
            if (result == DialogResult.OK)
            {
               // docToPrint.Print();
            }
        }

        private void printPreviewDialog1_Load(object sender, EventArgs e)
        {
            deviceErrorlistBox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + " " + "Print Preview Selected");
        }

        private void errorLogToolStripMenuItem_Click(object sender, EventArgs e)
        {            
            deviceErrorlistBox.Items.Clear();
        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deviceErrorlistBox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + " " + "Print Preview");
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deviceErrorlistBox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + " " + "Create New Configuration");
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deviceErrorlistBox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + " " + "File Close Menu Item Selected");
        }

        private void openWorkspaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deviceErrorlistBox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + " " + "Open Workspace Menu Item Selected");
        }

        private void closeWorkspaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deviceErrorlistBox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + " " + "Close Workspace Menu Item Selected");
        }

        private void recnetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deviceErrorlistBox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + " " + "Recent Items Menu Item Selected");
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void allToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //deviceErrorlistBox.Items.Clear();
            //deviceOutputlistBox.Items.Clear();
        }

        private void messageLogToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void listBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void deviceLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //deviceOutputlistBox.Items.Clear();
        }
    }
}
